
#ifndef ANIMAL_H_
#define ANIMAL_H_

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct Animal{
    char* name;
    int age;
    void (*Speek)(struct Animal*);
    void (*Dothing)(struct Animal*);
} Animal;


void DogSpeek(Animal* self){
    printf("%s Speek 汪汪汪 !\n", self->name);
}

void CatSpeek(Animal* self){
    printf("%s Speek 喵喵喵 !\n", self->name);
}

void DogDothing(Animal* self){
    printf("%s do see door !\n", self->name);
}

void CatDothing(Animal* self){
    printf("%s do catch mouse !\n", self->name);
}

Animal CreateDog(const char* name){
    Animal dog;
    dog.name = malloc(strlen(name) + 1);
    strcpy(dog.name, name);
    dog.name[strlen(name)] = '\0';
    dog.Speek = DogSpeek;
    dog.Dothing = DogDothing; 
    
    return dog;
}

Animal CreateCat(const char* name){
    Animal cat;
    cat.name = malloc(strlen(name) + 1);
    strcpy(cat.name, name);
    cat.name[strlen(name)] = '\0';
    cat.Speek = CatSpeek;
    cat.Dothing = CatDothing; 
    
    return cat;
}


#endif

int main(int argc, char* argv[]){
    Animal dog = CreateDog("Tom");
    Animal cat = CreateCat("Aim");

    dog.Speek(&dog);
    cat.Speek(&cat);

    dog.Dothing(&dog);
    cat.Dothing(&cat);
    
    // 释放内存
    free(dog.name);
    free(cat.name);

    return 0;
}